Shader "Custom/FlowingOutlineEnhanced"
{
    Properties
    {
        _MainTex ("Sprite Texture", 2D) = "white" {}
        _OutlineColor ("Outline Color", Color) = (1,0.5,0,1)  // 橙色火焰色
        _OutlineWidth ("Outline Pixel Width", Range(1,20)) = 3  // 描边像素宽度
        _FlowSpeed ("Flow Speed", Range(0,5)) = 1.0
        _AlphaThreshold ("Alpha Threshold", Range(0,1)) = 0.1  // 控制边缘检测的灵敏度
        _OutlineIntensity ("Outline Intensity", Range(0.5,2)) = 1.0  // 描边亮度增强
        _OutlineEnabled ("Outline Enabled", Float) = 1  // 描边开关，默认开
    }
    SubShader
    {
        // 使用和Sprite相同的渲染队列，保持排序一致
        Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
        Lighting Off
        ZWrite Off
        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha  // 标准透明混合

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _MainTex_TexelSize;  // 纹理像素尺寸 (1/宽, 1/高, 宽, 高)

            fixed4 _OutlineColor;
            float _OutlineWidth;
            float _FlowSpeed;
            float _AlphaThreshold;  // 可调节的alpha阈值
            float _OutlineIntensity;  // 描边强度
            float _OutlineEnabled; // 开关

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);

                // 如果描边开关关闭，直接返回原图
                if (_OutlineEnabled < 0.5)
                    return col;

                // 非透明区域直接返回原图（阈值可调节）
                if (col.a > _AlphaThreshold)
                    return col;

                float2 texelOffset = _MainTex_TexelSize.xy * _OutlineWidth;

                float flowTime = _Time.y * _FlowSpeed;
                float2 flowDir = float2(sin(flowTime), cos(flowTime)) * 0.5;

                float alphaSum = 0;
                float2 offsets[16] = {
                    float2(-texelOffset.x, 0), float2(texelOffset.x, 0),
                    float2(0, -texelOffset.y), float2(0, texelOffset.y),
                    float2(-texelOffset.x, -texelOffset.y), float2(texelOffset.x, texelOffset.y),
                    float2(-texelOffset.x, texelOffset.y), float2(texelOffset.x, -texelOffset.y),
                    float2(-texelOffset.x*0.7, -texelOffset.y*0.7), float2(texelOffset.x*0.7, texelOffset.y*0.7),
                    float2(-texelOffset.x*0.7, texelOffset.y*0.7), float2(texelOffset.x*0.7, -texelOffset.y*0.7),
                    float2(-texelOffset.x*1.5, 0), float2(texelOffset.x*1.5, 0),
                    float2(0, -texelOffset.y*1.5), float2(0, texelOffset.y*1.5)
                };

                [unroll]
                for (int idx = 0; idx < 16; idx++)
                {
                    float2 sampleUV = i.uv + offsets[idx] + flowDir * texelOffset.x;
                    sampleUV = clamp(sampleUV, 0, 1);
                    alphaSum += tex2D(_MainTex, sampleUV).a;
                }

                float avgAlpha = alphaSum / 16.0;
                if (avgAlpha > _AlphaThreshold * 0.5)
                {
                    float flicker = 0.7 + 0.3 * sin(flowTime * 15.0 + i.uv.y * 20.0);
                    fixed4 outlineCol = _OutlineColor * flicker * _OutlineIntensity;
                    outlineCol.a = max(flicker, 0.5) * _OutlineIntensity;
                    return outlineCol;
                }

                return col;
            }
            ENDCG
        }
    }
    FallBack "Sprites/Default"
}
